package com.jims.master.common.utils;

import com.google.common.collect.Lists;
import com.jims.master.common.mapper.JsonMapper;
import com.jims.master.modules.sys.dao.AreaMapper;
import com.jims.master.modules.sys.entity.Area;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2015/4/7.
 */
public class AreaUtils {
    private static final String CITYPATH= SystemPath.getSysPath() + "static" + SystemPath.getSeparator() + "js" + SystemPath.getSeparator() + "city" + SystemPath.getSeparator() + "area.json";
    private static AreaMapper areaMapper = SpringContextHolder.getBean(AreaMapper.class);
    public static List<Area> areaList() throws Exception {
        List<Area> areaList = new ArrayList<Area>();
        String path = StreamUtils.InputStreamTOString(new FileInputStream(new File(CITYPATH)), "utf-8");
        JsonMapper jsonMapper = new JsonMapper();
        areaList = jsonMapper.fromJson(path, jsonMapper.createCollectionType(ArrayList.class, Area.class));
        return areaList;
    }

    public static Map areaMap() throws Exception {
        List<Area> areaList = new ArrayList<Area>();
        String path = StreamUtils.InputStreamTOString(new FileInputStream(new File(CITYPATH)), "utf-8");
        JsonMapper jsonMapper = new JsonMapper();
        areaList = jsonMapper.fromJson(path, jsonMapper.createCollectionType(ArrayList.class, Area.class));
        Map map = new HashMap();
        for (Area area : areaList) {
            map.put(area.getCode(), area);
        }
        return map;
    }

    /**
     * 根据code获取地址
     *
     * @param code
     * @return
     */
    public static String reverseArea(String code) {
        Map map = null;
        String address = "";
        try {
            if(StringUtils.isNotEmpty(code)){
                map = areaMap();
                Area area = (Area) map.get(code);
                address = getAddressArea(area, address, map);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return address;
    }

    public static String getAddressArea(Area area, String address, Map map) {
        String add = "";
        if (StringUtils.isNotEmpty(area.getParentCode())) {
            Area a = (Area) map.get(area.getParentCode());
            add = getAddressArea(a, address, map);
        }
        address = add + area.getName();
        return address;
    }

    public static String areaProvince() {
        List<Area> areaList = null;
        try {
            areaList = areaList();
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<Area> provinceList = Lists.newArrayList();
        for (Area area : areaList) {
            if (area.getParentCode().equals("") && area.getType().equals("1")) {
                provinceList.add(area);
            }
        }
        return ulStr(provinceList, "province");
    }

    public static String areaCity(String code) throws Exception {
        List<Area> areaList = areaList();
        List<Area> cityList = Lists.newArrayList();
        for (Area area : areaList) {
            if (area.getParentCode().equals(code)) {
                cityList.add(area);
            }
        }
        return ulStr(cityList, "city");
    }

    public static String areaCounty(String code) throws Exception {
        List<Area> areaList = areaList();
        List<Area> countyList = Lists.newArrayList();
        for (Area area : areaList) {
            if (area.getParentCode().equals(code)) {
                countyList.add(area);
            }
        }
        return ulStr(countyList, "county");
    }

    private static String ulStr(List<Area> areaList, String method) {
        String areaStr = "";
        if (areaList.size()>0){
            String provinceUl = "<ul class='block' id='" + method + "IdUl'>";
            String li_a_h = "<li class='letter'>A-G</li>";
            String li_i_o = "<li class='letter'>H-N</li>";
            String li_p_u = "<li class='letter'>Q-T</li>";
            String li_v_z = "<li class='letter'>U-Z</li>";
            for (Area area : areaList) {
                if (area.getGroup().matches("[A-G]")) {
                    li_a_h = li_a_h + "<li><a href=\"javascript:void(0);\" onclick=\"click_" + method + "(" + area.getCode() + ",'" + area.getName() + "')\">" + area.getName() + "</a></li>";
                } else if (area.getGroup().matches("[H-N]")) {
                    li_i_o = li_i_o + "<li><a href=\"javascript:void(0);\" onclick=\"click_" + method + "(" + area.getCode() + ",'" + area.getName() + "')\">" + area.getName() + "</a></li>";
                } else if (area.getGroup().matches("[Q-T]")) {
                    li_p_u = li_p_u + "<li><a href=\"javascript:void(0);\" onclick=\"click_" + method + "(" + area.getCode() + ",'" + area.getName() + "')\">" + area.getName() + "</a></li>";
                } else if (area.getGroup().matches("[U-Z]")) {
                    li_v_z = li_v_z + "<li><a href=\"javascript:void(0);\" onclick=\"click_" + method + "(" + area.getCode() + ",'" + area.getName() + "')\">" + area.getName() + "</a></li>";
                }
            }
            areaStr = provinceUl + li_a_h + li_i_o + li_p_u + li_v_z + "</ul><div class='clear'></div>";
        }

        return areaStr;
    }

    /**
     * @param  '[ids]' 传递参数
     * @return java.lang.String 返回类型
     * @throws
     * @Title: getAreaLabel
     * @Description: (翻译市级多选)
     * @author CTQ
     * @date 2015-11-17 14:19
     */
    public static String getAreaLabel(String ids) {
        String names = "";
        if(StringUtils.hasLength(ids)){
            if(ids.contains(",")){
                String[] strs = ids.split(",");
                for(String str : strs){
                    names+=areaMapper.get(str).getName()+",";
                }
            }else{
                names=areaMapper.get(ids).getName();
            }
        }

        return names;
    }

    public static String getAreaName(String ids) {
        String names = "";
        Area area = new Area();
        if(StringUtils.hasLength(ids)){
            if(ids.contains(",")){
                String[] strs = ids.split(",");
                for(String str : strs){
                    area = areaMapper.get(str);
                    Area city = areaMapper.get(area.getParentCode());
                    Area province = areaMapper.get(city.getParentCode());
                    names+=province.getName()+city.getName()+area.getName()+",";
                }
            }else{
                area = areaMapper.get(ids);
                Area city = areaMapper.get(area.getParentCode());
                Area province = areaMapper.get(city.getParentCode());
                names=province.getName()+city.getName()+area.getName();
            }
        }

        return names;
    }
}
